package pos.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import empresas.Empresa;

public class JDBCEmpresaDAO implements IEmpresaDAO {
	
private Connection conn;
	
	public JDBCEmpresaDAO(){
		
		conn = ConnectionManager.getInstance().checkOut();
		
		
	}

	@Override
	public void delete(String cif) {
		String sql = "DELETE FROM empresas WHERE (cifempresa = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, cif);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
		
	}

	@Override
	public void insert(Empresa e) {
		String sql = "INSERT INTO Empresas (cifempresa, nombre, direccion, telefono) VALUES (?, ?, ?, ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, e.getCifempresa());
            stmt.setString(2, e.getNombre());
            stmt.setString(3, e.getDireccion());
            stmt.setString(4, e.getTelefono());
            stmt.executeUpdate();

        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ex) {
            }
        }
		
	}

	@Override
	public void modificaempresa(Empresa e) {
		PreparedStatement stmt = null;
		ResultSet result = null;
		String sql = "UPDATE empresas SET nombre = ?, direccion = ?, telefono = ?" +
				"WHERE (cifempresa = ?)";
		
		try{
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, e.getNombre());
			stmt.setString(2, e.getDireccion());
            stmt.setString(3, e.getTelefono());            
            stmt.setString(4, e.getCifempresa());
			
			stmt.executeUpdate();
		}catch(SQLException ex){
			System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ex) {
            }
        }
		
	}

	@Override
	public Empresa selectempresa(String cif) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        Empresa e = null;      
        String sql = "SELECT * FROM empresas WHERE (cifmpresa = ?) ";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, cif);
            result = stmt.executeQuery();

            if(result.next()){
            	e = new Empresa();
            	e.setCifempresa(result.getString("cifempresa"));
            	e.setNombre(result.getString("nombre"));
            	e.setDireccion(result.getString("direccion"));
            	e.setTelefono(result.getString("telefono"));        	

            }
        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ex) {
            }
        }
        return e;
	}

	@Override
	public List<Empresa> selectempresas() {
		PreparedStatement stmt = null;
        ResultSet result = null;        
        List<Empresa> listaempresas = new LinkedList<Empresa>();
        
        String sql = "SELECT * FROM empresas ORDER BY cifempresa";
    
        try {
            stmt = conn.prepareStatement(sql);
            result = stmt.executeQuery();
            while (result.next()) {
            	Empresa e = new Empresa();
            	e.setCifempresa(result.getString("cifempresa"));
            	e.setNombre(result.getString("nombre"));
            	e.setDireccion(result.getString("direccion"));
            	e.setTelefono(result.getString("telefono"));
                listaempresas.add(e);
            }
        } catch (SQLException ex) {
            System.out.println("Message: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("ErrorCode: " + ex.getErrorCode());
        } finally {
            try {
            	conn.close();
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException ex) {
            }
        }
        return listaempresas;
	}

	
	public void finalize(){
		ConnectionManager.getInstance().checkIn(conn);
		
	}

}
